#coding=utf-8
import hashlib

import rsa
from rsa import PublicKey, common, transform, core


PUBLIC_KEY = """
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAMAY6NB3U1kT6D5zs2aWyhiYhCGkLZu9A3J1v128vTAwiLKoJ21GOFJ9
14pjfXizUSDq4kiAFxXjCEvqFO5/eW8zpL9xipRRSB2WG70rlHOzGrVS8LdyRZQc
vCLXnguGpA2gamIkcifE7PsJHVygkUS+sGK3r1aWEXDhyB5BMlbnAgMBAAE=
-----END RSA PUBLIC KEY-----
"""

# PUBLIC_KEY = """
# -----BEGIN RSA PUBLIC KEY-----
# MIGJAoGBAIJ3tjibcM9y4bhsBGe8sYu1D5TrYNsoSzxxzCXXzcYy/zhVJjrrIIoS
# RYqjPlzIqDsO0oh3P17jRAJZBJZVcItBuGjeYJIsIGV/KwCG+EfuOdmJW7ZfrZcC
# dKUYA4uwnGPrLgidgir5QL/wQhBbvY2RM5uamLnu2iTr+gW0X6INAgMBAAE=
# -----END RSA PUBLIC KEY-----
# """

public_key = PublicKey.load_pkcs1(PUBLIC_KEY)

en_hex = '7696979e73701f75de6b7e5bd91580d5704b9202ca5ea2a80ba4efec1913b370c990e82b3633f3bef1fed0ada994d70ac26b034182be669ce1c376fe5686d811e934ce5bcaacb669a734b6148431fcc7224024d0ae17db022221f8a93b1ed8abf66900430a2008e9c9e5e93bd7b47f89e74236f380fccc43158433c4a9b378bf'


def decode_hex(en_hex,real_len):
    en_bytes = bytes.fromhex(en_hex)
    de_hex = ''
    #需要128位分开解密

    en_len = int(len(en_bytes))

    for i in range(int(en_len/128)):
        encrypted = transform.bytes2int(en_bytes[i*128:i*128+128])
        decrypted = core.decrypt_int(encrypted, public_key.e, public_key.n)
        de_bytes = transform.int2bytes(decrypted)
        if i < int(len(en_bytes)/128)-1:
            de_hex += de_bytes[-117:].hex()
            print(de_bytes[-117:].hex())
        else:
            r_len = divmod(real_len,117)[1]
            de_hex += de_bytes[-r_len:].hex()

    return de_hex

def encode_hex(de_hex):
    return rsa.encrypt(bytes.fromhex(de_hex), public_key).hex()


def getMd5(hex_data):
    myhash = hashlib.md5()
    myhash.update(bytes.fromhex(hex_data))
    return myhash.hexdigest()

#en_data = '759432121836c2308c70a6cc3039650e1695704ad77ed39bda932157ff9f613c6169ae34e79c1f5803967de7ec450fe59316a9972f91e8e05ca9368ab485404dd2a1b794a7c67f3fd703f114896bcfb68da3fb992b608bd334cd1508ce5ee49983dde26dbf50c44d148f4a5cb9cf6d47da48f017e5bbcef3b9750a63dd69885755166b9436ee23b4baec256aca37e7a7f262bb880f9ee327b1116fd9f4cbe551550bd4517b0690350516a64fd69a7edc393882323afcc4af2f7111a4edf6faf4f2e6830b586e726f0366d473abbdd5373ab04e075e16c0bb9fe93f30d8175e99894b3d404e2d096277869d7999963c1a166ce2d373fc49c6601437d0fa2461c7'
#print(encode_hex('006d00000010'))
#print en_data
#print decode_hex(en_data,127)
# print getMd5('00')
#420060120f0d0315a23ae2b2dbe8c4490e3e0c18c2bffe958496bd7215a2a1c423173b2b6980056a5d1a14d9ee5f123890cc9475dff70a0df4cf576f917134ca92eccf4e930ea1eb44e065bb15ad4cd8f3a44066c5f7e8194762a87f595b3ee2cbddca9639b92da2311d90ba826ade0d4c33bb70c61e65d2efa4c8bc2547d86d6f4033877e67c301a7396c6df9022902771a2cae6f9fb8445e9624496a9854f21901eb44f9243c580d457513c682040f631eae62eecab0c25f714d3e4e0d4bc0021c9fd6878c3bd4176e3d4d50358dae6be0fbfa0d9a8ee908e46df186de6f0bd623a92529b3067ae94ef5bdf652cca86b056f83edc450911e66345f986e6a82
# data_de = decode_hex("51f6526b8e3de3349b4a921cab0aa9223950dda1150841eb3edbe1d1d00e01618a9755c2c75c3b5c734e569573df9df53244c4b24c79b34053d95f93d164deceaa464c7d4ba40d93ca44eb9c15d5419dafebf15e0f420d135ea831fe9fe3c3c39174fcc202b473f0589852e637ba8d3e5907565e71106d13d2bb2f547025dcdc3868c9c45e7bcc5a6858d63dbf02d7e9fcf29916e02b8e8ce0e0e2d65f7b66e73027a00dd4d07f681b508bc0ec18c053504c2e55021270c3d9b6a2f8d396bc52b78ac60239ce07f5c34ac97bcd037b83dfefab26a6fc219fc920b430054550867aa4d6b9c8ecf394bd132ca2b7cc87e98a75eea17844b968a72a9878ffaa8973",132)
# print(data_de)
# data ='127b64094bbc8bdc41044b7d02e1e1196e2cce5feb3182f7216cb8180dbca026a74e6ca6cc17eef606bf04b1b31f31f3a3b4c761dd1e1da3e137ed43dbd81383811f65252eef54acb0cf32582511e4aa8d3cb55bfc4733d190d8ff18ee575b02ef67b22d4a79a9e6dbfc22cf98fd991ae2e7b83a8beed635092d4b0974afc160f7'
# de_data=de_trans_spec(data)
# en_data = en_trans_spec(de_data)
# print en_data